home *** CD-ROM | disk | FTP | other *** search
- package com.sun.java.swing.plaf.basic;
-
- import com.sun.java.swing.JComponent;
- import com.sun.java.swing.UIManager;
- import com.sun.java.swing.border.Border;
- import com.sun.java.swing.plaf.TextUI;
- import com.sun.java.swing.plaf.UIResource;
- import com.sun.java.swing.text.BadLocationException;
- import com.sun.java.swing.text.Caret;
- import com.sun.java.swing.text.DefaultCaret;
- import com.sun.java.swing.text.DefaultEditorKit;
- import com.sun.java.swing.text.DefaultHighlighter;
- import com.sun.java.swing.text.Document;
- import com.sun.java.swing.text.EditorKit;
- import com.sun.java.swing.text.Element;
- import com.sun.java.swing.text.Highlighter;
- import com.sun.java.swing.text.JTextComponent;
- import com.sun.java.swing.text.Keymap;
- import com.sun.java.swing.text.View;
- import com.sun.java.swing.text.ViewFactory;
- import java.awt.Color;
- import java.awt.Component;
- import java.awt.Container;
- import java.awt.Dimension;
- import java.awt.Font;
- import java.awt.Graphics;
- import java.awt.Insets;
- import java.awt.Point;
- import java.awt.Rectangle;
- import java.awt.Shape;
- import java.beans.PropertyChangeEvent;
- import java.io.IOException;
- import java.io.ObjectInputStream;
- import java.io.Serializable;
-
- public abstract class BasicTextUI extends TextUI implements ViewFactory, Serializable {
- private static final EditorKit defaultKit = new DefaultEditorKit();
- transient JTextComponent editor;
- transient boolean painted = false;
- transient RootView rootView = new RootView(this);
- transient UpdateHandler updateHandler = new UpdateHandler(this);
-
- public View create(Element elem) {
- return null;
- }
-
- public View create(Element elem, int p0, int p1) {
- return null;
- }
-
- protected Caret createCaret() {
- return new DefaultCaret();
- }
-
- protected Highlighter createHighlighter() {
- return new DefaultHighlighter();
- }
-
- protected Keymap createKeymap() {
- String nm = this.getKeymapName();
- Keymap map = JTextComponent.getKeymap(nm);
- if (map == null) {
- Keymap parent = JTextComponent.getKeymap("default");
- map = JTextComponent.addKeymap(nm, parent);
- String prefix = this.getPropertyPrefix();
- Object o = UIManager.get(prefix + ".keyBindings");
- if (o != null && o instanceof JTextComponent.KeyBinding[]) {
- JTextComponent.KeyBinding[] bindings = (JTextComponent.KeyBinding[])o;
- JTextComponent.loadKeymap(map, bindings, this.getComponent().getActions());
- }
- }
-
- return map;
- }
-
- public void damageRange(int p0, int p1) {
- if (this.painted) {
- Rectangle alloc = this.getVisibleEditorRect();
-
- try {
- Shape s0 = this.rootView.modelToView(p0, alloc);
- Shape s1 = this.rootView.modelToView(p1, alloc);
- if (s0 != null && s1 != null) {
- Rectangle r0 = s0.getBounds();
- Rectangle r1 = s1.getBounds();
- if (r0.y == r1.y) {
- this.editor.repaint(r0.x, r0.y, r1.x - r0.x + 1, r0.height);
- } else {
- this.editor.repaint(alloc.x, r0.y, alloc.width, r1.y - r0.y + r1.height);
- }
- }
- } catch (BadLocationException var8) {
- }
- }
-
- }
-
- protected final JTextComponent getComponent() {
- return this.editor;
- }
-
- public Insets getDefaultMargin() {
- return new Insets(0, 0, 0, 0);
- }
-
- public EditorKit getEditorKit() {
- return defaultKit;
- }
-
- protected String getKeymapName() {
- String nm = this.getClass().getName();
- int index = nm.lastIndexOf(46);
- if (index >= 0) {
- nm = nm.substring(index + 1, nm.length());
- }
-
- return nm;
- }
-
- public Dimension getMaximumSize(JComponent c) {
- Insets i = c.getInsets();
- long width = this.rootView.getResizeWeight(0) > 0 ? 2147483647L : Math.min((long)this.rootView.getPreferredSpan(0) + (long)i.left + (long)i.right, 2147483647L);
- long height = this.rootView.getResizeWeight(1) > 0 ? 2147483647L : Math.min((long)this.rootView.getPreferredSpan(1) + (long)i.top + (long)i.bottom, 2147483647L);
- return new Dimension((int)width, (int)height);
- }
-
- public Dimension getMinimumSize(JComponent c) {
- Insets i = c.getInsets();
- long width = this.rootView.getResizeWeight(0) > 0 ? 1L : Math.min((long)this.rootView.getPreferredSpan(0) + (long)i.left + (long)i.right, 2147483647L);
- long height = this.rootView.getResizeWeight(1) > 0 ? 1L : Math.min((long)this.rootView.getPreferredSpan(1) + (long)i.top + (long)i.bottom, 2147483647L);
- return new Dimension((int)width, (int)height);
- }
-
- public Dimension getPreferredSize(JComponent c) {
- Insets i = c.getInsets();
- Dimension d = ((Component)c).getSize();
- if (d.width > i.left + i.right && d.height > i.top + i.bottom) {
- this.rootView.setSize((float)(d.width - i.left - i.right), (float)(d.height - i.top - i.bottom));
- }
-
- d.width = (int)Math.min((long)this.rootView.getPreferredSpan(0) + (long)i.left + (long)i.right, 2147483647L);
- d.height = (int)Math.min((long)this.rootView.getPreferredSpan(1) + (long)i.top + (long)i.bottom, 2147483647L);
- return d;
- }
-
- protected abstract String getPropertyPrefix();
-
- public View getRootView() {
- return this.rootView;
- }
-
- protected Rectangle getVisibleEditorRect() {
- Rectangle alloc = new Rectangle(this.editor.getSize());
- Insets insets = this.editor.getInsets();
- alloc.x += insets.left;
- alloc.y += insets.top;
- alloc.width -= insets.left + insets.right;
- alloc.height -= insets.top + insets.bottom;
- return alloc;
- }
-
- protected void installDefaults(JComponent c) {
- String prefix = this.getPropertyPrefix();
- Font f = this.editor.getFont();
- if (f == null || f instanceof UIResource) {
- this.editor.setFont(UIManager.getFont(prefix + ".font"));
- }
-
- Color bg = this.editor.getBackground();
- if (bg == null || bg instanceof UIResource) {
- this.editor.setBackground(UIManager.getColor(prefix + ".background"));
- }
-
- Color fg = this.editor.getForeground();
- if (fg == null || fg instanceof UIResource) {
- this.editor.setForeground(UIManager.getColor(prefix + ".foreground"));
- }
-
- Color color = this.editor.getCaretColor();
- if (color == null || color instanceof UIResource) {
- this.editor.setCaretColor(UIManager.getColor(prefix + ".caretForeground"));
- }
-
- Color s = this.editor.getSelectionColor();
- if (s == null || s instanceof UIResource) {
- this.editor.setSelectionColor(UIManager.getColor(prefix + ".selectionBackground"));
- }
-
- Color sfg = this.editor.getSelectedTextColor();
- if (sfg == null || sfg instanceof UIResource) {
- this.editor.setSelectedTextColor(UIManager.getColor(prefix + ".selectionForeground"));
- }
-
- Color dfg = this.editor.getDisabledTextColor();
- if (dfg == null || dfg instanceof UIResource) {
- this.editor.setDisabledTextColor(UIManager.getColor(prefix + ".inactiveForeground"));
- }
-
- Border b = this.editor.getBorder();
- if (b == null || b instanceof UIResource) {
- this.editor.setBorder(UIManager.getBorder(prefix + ".border"));
- }
-
- Caret caret = this.createCaret();
- this.editor.setCaret(caret);
- Object o = UIManager.get(prefix + ".caretBlinkRate");
- if (o != null && o instanceof Integer) {
- Integer rate = (Integer)o;
- caret.setBlinkRate(rate);
- }
-
- }
-
- protected void installListeners(JComponent c) {
- }
-
- public void installUI(JComponent c) {
- if (c instanceof JTextComponent) {
- this.editor = (JTextComponent)c;
- this.installDefaults(c);
- this.editor.setOpaque(true);
- this.editor.setAutoscrolls(true);
- this.editor.setHighlighter(this.createHighlighter());
- this.editor.addPropertyChangeListener(this.updateHandler);
- Document doc = this.editor.getDocument();
- if (doc == null) {
- this.editor.setDocument(this.getEditorKit().createDefaultDocument());
- } else {
- doc.addDocumentListener(this.updateHandler);
- this.modelChanged();
- }
-
- this.installListeners(c);
- this.editor.setKeymap(this.createKeymap());
- } else {
- throw new Error("TextUI needs JTextComponent");
- }
- }
-
- protected void modelChanged() {
- ViewFactory f = this.rootView.getViewFactory();
- Document doc = this.editor.getDocument();
- Element elem = doc.getDefaultRootElement();
- this.setView(f.create(elem));
- }
-
- public Rectangle modelToView(int pos) throws BadLocationException {
- if (this.painted) {
- Rectangle alloc = this.getVisibleEditorRect();
- Shape s = this.rootView.modelToView(pos, alloc);
- return s.getBounds();
- } else {
- return null;
- }
- }
-
- public final void paint(Graphics g, JComponent c) {
- if (this.rootView.getViewCount() > 0 && this.rootView.getView(0) != null) {
- Runnable painter = new SafePainter(this, g);
- Document doc = this.editor.getDocument();
- doc.render(painter);
- }
-
- }
-
- protected void paintBackground(Graphics g) {
- g.setColor(this.editor.getBackground());
- Dimension d = this.editor.getSize();
- g.fillRect(0, 0, d.width, d.height);
- }
-
- protected void paintSafely(Graphics g) {
- this.painted = true;
- Highlighter highlighter = this.editor.getHighlighter();
- Caret caret = this.editor.getCaret();
- if (this.editor.isOpaque()) {
- this.paintBackground(g);
- }
-
- if (highlighter != null) {
- highlighter.paint(g);
- }
-
- Rectangle alloc = this.getVisibleEditorRect();
- this.rootView.paint(g, alloc);
- if (caret != null) {
- caret.paint(g);
- }
-
- }
-
- protected void propertyChange(PropertyChangeEvent evt) {
- }
-
- private void readObject(ObjectInputStream s) throws ClassNotFoundException, IOException {
- s.defaultReadObject();
- this.rootView = new RootView(this);
- this.updateHandler = new UpdateHandler(this);
- }
-
- protected final void setView(View v) {
- this.rootView.setView(v);
- this.painted = false;
- this.editor.invalidate();
- }
-
- protected void uninstallDefaults(JComponent c) {
- if (this.editor.getCaretColor() instanceof UIResource) {
- this.editor.setCaretColor((Color)null);
- }
-
- if (this.editor.getSelectionColor() instanceof UIResource) {
- this.editor.setSelectionColor((Color)null);
- }
-
- if (this.editor.getDisabledTextColor() instanceof UIResource) {
- this.editor.setDisabledTextColor((Color)null);
- }
-
- if (this.editor.getSelectedTextColor() instanceof UIResource) {
- this.editor.setSelectedTextColor((Color)null);
- }
-
- if (this.editor.getBorder() instanceof UIResource) {
- this.editor.setBorder((Border)null);
- }
-
- this.editor.setCaret((Caret)null);
- }
-
- protected void uninstallListeners(JComponent c) {
- }
-
- public void uninstallUI(JComponent c) {
- this.editor.removePropertyChangeListener(this.updateHandler);
- this.editor.getDocument().removeDocumentListener(this.updateHandler);
- this.painted = false;
- this.uninstallDefaults(c);
- this.editor.setHighlighter((Highlighter)null);
- this.rootView.setView((View)null);
- ((Container)c).removeAll();
- this.editor.setKeymap((Keymap)null);
- this.uninstallListeners(c);
- }
-
- public int viewToModel(Point pt) {
- if (this.painted) {
- Rectangle alloc = this.getVisibleEditorRect();
- return this.rootView.viewToModel((float)pt.x, (float)pt.y, alloc);
- } else {
- return -1;
- }
- }
- }
-